/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

// * @author Progresywny Maciek
public class Erosion {

    BufferedImage out;
    BufferedImage localIn;
    private int sizeFilter = 1;

    public void setNumberOfNeighbours(int num) {
        //being odd
        if (num % 2 != 0) {
            sizeFilter = (num - 1) / 2;
        }
    }

    public BufferedImage getErosionResult() {
        return out;
    }

    public Erosion(BufferedImage in) {
        //localIn = in;
        //initing and copying the image
        localIn = new BufferedImage(in.getWidth(), in.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = localIn.createGraphics();
        //drawing the input image inside the new image
        g2d.drawImage(in, 0, 0, null);

        //init output Image
        out = new BufferedImage(in.getWidth(), in.getHeight(),
                BufferedImage.TYPE_INT_ARGB);
        for (int w = 0; w < in.getWidth(); w++) {
            for (int h = 0; h < in.getHeight(); h++) {
                out.setRGB(w, h, 0x0);
            }
        }
    }

    public void runErosionWidth() {
        //no automatic , only doing 
        for (int h = 0; h < out.getHeight(); h++) {
            for (int w = sizeFilter; w < (out.getWidth() - sizeFilter); w++) {
                int accNeigh = 0;
                for (int i = -sizeFilter; i < 0; i++) {
                    accNeigh = accNeigh + localIn.getRGB(w + i, h);
                }
                for (int i = 1; i < (sizeFilter + 1); i++) {
                    accNeigh = accNeigh + localIn.getRGB(w + i, h);
                }
                if (accNeigh == (sizeFilter + 1) * 0xFF0000FF) {
                    out.setRGB(w, h, 0xFF0000FF);
                } else {
                    out.setRGB(w, h, 0x0);
                }

            }//w
        }//h

    }//runErosionWidth

    public void runErosionHeight() {
        for (int w = 0; w < (out.getWidth()); w++) {
            for (int h = sizeFilter; h < (out.getHeight() - sizeFilter); h++) {
                int accNeigh = 0;
                for (int i = -sizeFilter; i < 0; i++) {
                    accNeigh = accNeigh + localIn.getRGB(w, h + i);
                }
                for (int i = 1; i < (sizeFilter + 1); i++) {
                    accNeigh = accNeigh + localIn.getRGB(w, h + i);
                }
                if (accNeigh == (sizeFilter + 1) * 0xFF0000FF) {
                    out.setRGB(w, h, 0xFF0000FF);
                } else {
                    out.setRGB(w, h, 0x0);
                }
            }//w
        }//h

    }

    public void runErosion() {
        //no automatic , only doing 
        for (int h = sizeFilter; h < (out.getHeight() - sizeFilter); h++) {
            for (int w = sizeFilter; w < (out.getWidth() - sizeFilter); w++) {
                int accNeigh = 0;
                int total = 0;
                for (int i = -sizeFilter; i < 0; i++) {
                    for (int j = -sizeFilter; j < 0; j++) {
                        accNeigh = accNeigh + localIn.getRGB(w + i, h + j);
                        total++;
                    }
                }
                for (int i = 1; i < (sizeFilter + 1); i++) {
                    for (int j = 1; j < (sizeFilter + 1); j++) {
                        accNeigh = accNeigh + localIn.getRGB(w + i, h + j);
                        total++;
                    }
                }

                if (accNeigh == (total) * 0xFF0000FF) {
                    out.setRGB(w, h, 0xFF0000FF);
                } else {
                    out.setRGB(w, h, 0x0);
                }

            }//w
        }//h 
    }//runErosion
}//class
